package StudyCode.summary.csv;

import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class StringArrayToCSVRecord {
	public static final String CSV_SEP = ",";

	public static void main(String[] args) {
		String[] strings = new String[3];
		strings[0] = "www";
		strings[1] = "tah1986";
		strings[2] = "com";
		System.out.println("String 数组:" + Arrays.toString(strings));
		System.out.println("转为CSV记录");
		System.out.println(toCsvRecord(strings));
	}

	public static String toCsvRecord(String... fields) {
		StringBuilder buf = new StringBuilder();
		if (fields != null) {
			for (int i = 0; i < fields.length; i++) {
				if (i != 0) {
					buf.append(CSV_SEP);
				}
				buf.append(enclose(fields[i]));
			}
		}
		return buf.toString();
	}

	public static String enclose(String field) {
		return "\"" + field + "\"";
	}
}
